﻿package test;

/**
 * 枚举的几种用法
 * */
public class LangDemo4 {
	public static void main(String[] args) {
		System.out.println(ColorX.GREEN);
		System.out.println(ColorY.getName(3));
		Color3.GREEN.printColor();
	}
}
enum ColorX{
	RED,YELLOW,GREEN;
}
enum ColorY{
	RED("红色",1),YELLOW("黄色",2),GREEN("绿色",3);
	private String name;
	private int index;
	ColorY(String name,int index){
		this.name=name;
		this.index=index;
	}
	public static ColorY getName(int index){
		ColorY c2 = null;
		for(ColorY c:ColorY.values()){
			if(c.getIndex()==index){
				c2=c;
				break;
			}
		}
		return c2;
	}
	@Override
	public String toString() {
		return index+":"+name;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
}
enum Color3 implements Colors{
	RED("红色",1),YELLOW("黄色",2),GREEN("绿色",3);
	private String name;
	private int index;
	
	Color3(String name,int index){
		this.name=name;
		this.index=index;
	}

	@Override
	public void printColor() {
		System.out.println(index+":"+name);
	}
	
}

interface Colors{
	void printColor();
}
